package com.self.audiocapture.move;

import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * @author huang
 */
public class MoveControl implements IMoveControl {
    private static final String TAG = "MoveControl";
    private boolean move = false;
    private long mLastDownTime = -1;
    private WindowManager wm;
    private LinearLayout linearLayout;
    private WindowManager.LayoutParams layoutParams;
    private CallBack callBack;

    /**
     * View的坐标，与判断移动时的坐标 的横坐标的差值
     */
    private int horizontalOffset;
    private int verticalOffset;

    private int longClickLimit = 100;

    /**
     * 最大甩的距离，距离越小，越容易触发
     */
    private int throwMaxDistance = 50;

    public MoveControl(WindowManager wm, LinearLayout linearLayout, WindowManager.LayoutParams layoutParams, CallBack callBack, int throwMaxDistance) {
        this.wm = wm;
        this.linearLayout = linearLayout;
        this.layoutParams = layoutParams;
        this.callBack = callBack;
        this.throwMaxDistance = throwMaxDistance;
    }

    public MoveControl(WindowManager wm, LinearLayout linearLayout, WindowManager.LayoutParams layoutParams, CallBack callBack) {
        this.wm = wm;
        this.linearLayout = linearLayout;
        this.layoutParams = layoutParams;
        this.callBack = callBack;
    }

    @Override
    public boolean isLongTouch() {
        long time = System.currentTimeMillis();
        //需要满足该move事件与down之间之间的时间差大于阈值
        if ((time - mLastDownTime >= longClickLimit)) {
            return true;
        }
        return false;
    }

    @Override
    public void setLongTouchTime(int time) {
        longClickLimit = time;
    }

    @Override
    public boolean isMove() {
        return move;
    }

    @Override
    public void setMove(boolean move, MotionEvent event) {
        this.move = move;
        if(move){
            horizontalOffset = ((int) (event.getRawX() - layoutParams.x));
            verticalOffset = ((int) (event.getRawY() - layoutParams.y));
        }

        if(isThrow && callBack!=null){
            float distance = ((float) Math.sqrt(Math.pow(event.getX() - lastMovePointF.x, 2) + Math.pow(event.getY() - lastMovePointF.y, 2)));
            if(distance >= throwMaxDistance){
                // 右边
                if(event.getX() <= lastMovePointF.x){
                    callBack.toRight(callBack);

                    // 左边
                } else {
                    callBack.toLeft(callBack);
                }
            }
            isThrow = false;
        }
    }

    @Override
    public void updateLastDownTime() {
        mLastDownTime = System.currentTimeMillis();
    }

    /**
     * 移动的最后一个点的坐标
     */
    PointF lastMovePointF = new PointF();

    /**
     * 是否有甩的动作
     */
    boolean isThrow = false;
    @Override
    public void move(IMoveControl moveControl, MotionEvent event) {
        if(move){
            isThrow = true;
            layoutParams.x = (int) event.getRawX() - horizontalOffset;
            layoutParams.y = (int) event.getRawY() - verticalOffset;
            wm.updateViewLayout(linearLayout, layoutParams);
            lastMovePointF.set(event.getX(), event.getY());
        }
    }

    public interface CallBack {
        void toLeft(CallBack callBack);
        void toRight(CallBack callBack);
    }
}